package org.example.code.mobile.popup.actions;


import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.core.filesystem.EFS;

public class OpenFileInEditorAction implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public OpenFileInEditorAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		

		/*	
		 * open a file in the eclipse editor
		 * 
		 * add two plugin dependence
		 * 
		 * org.eclipse.ui.ide;bundle-version="3.9.1",
 		 * org.eclipse.core.filesystem;bundle-version="1.4.0"
 		 * 
 		 *  import org.eclipse.core.resources.IFile;
			import org.eclipse.core.runtime.IPath;
			import org.eclipse.jface.viewers.TreePath;
			import org.eclipse.jface.viewers.TreeSelection;
			import org.eclipse.ui.IWorkbenchPage;
			import org.eclipse.ui.IWorkbenchWindow;
			import org.eclipse.ui.PlatformUI;
			import org.eclipse.ui.ide.IDE;
			import org.eclipse.core.filesystem.EFS;
*/
		try {

  		IWorkbenchWindow window = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow();

			ISelection selection = window.getSelectionService().getSelection(
					"org.eclipse.ui.navigator.ProjectExplorer");

			TreePath[] paths = ((TreeSelection) selection).getPaths();
			TreePath p = paths[0];
			Object last = p.getLastSegment();

			if (last instanceof IFile) {
				IPath ipath = ((IFile) last).getLocation();

				IWorkbenchPage page = window.getActivePage();

				IDE.openEditorOnFileStore(page, EFS.getLocalFileSystem()
						.getStore(ipath));
			}
		} catch (Exception blah) {
		}


		MessageDialog.openInformation(shell, "Mobile Application Developer",
				"Generate Mobil Application.");
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
